﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Nova_MOD;
using Nova_Common.Extend;

namespace Nova_BLL
{
    public class Users
    {
        int i = 0;
        string vUserinfo = "";

        Nova_DAL.User Nova_DAL_Users = new Nova_DAL.User();
        DataSet ds = null;

        ///<summary>
        ///判断用户名是否存在
        ///</summary>
        ///<param name="sQL">vUserNo ,vPassWord</param>
        ///<returns>返回值: 1成功,0失败</returns>
        ///Writer：[王成友]
        ///Create Date： [2010-5-19]
        ///</summary>
        public int Exists(Nova_MOD.Users Users)
        {
            try
            {
                i = Nova_DAL_Users.Exist(Users);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return i;

        }


        ///<summary>
        ///查找用户名名称
        ///</summary>
        ///<param name="sQL">vUserNo </param>
        ///<returns>返回用户名称</returns>
        ///Writer：[王成友]
        ///Create Date： [2010-5-19]
        ///</summary>
        public string CallBackUserName(string vUserNo)
        {
            try
            {
                vUserinfo = Nova_DAL_Users.CallBackUserName(vUserNo);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return vUserinfo;
        }

        ///<summary>
        ///查找用户部门
        ///</summary>
        ///<param name="sQL">vUserNo </param>
        ///<returns>返回用户部门</returns>
        ///Writer：[王成友]
        ///Create Date： [2010-5-19]
        ///</summary>
        public string CallBackDepartment(string vUserNo)
        {
            try
            {
                vUserinfo = Nova_DAL_Users.CallBackDepartment(vUserNo);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return vUserinfo;
        }

        ///<summary>
        ///查找用户工作组编号
        ///</summary>
        ///<param name="sQL">vGroupNo </param>
        ///<returns>返回工作组编号: 如果返回0失败</returns>
        ///Writer：[王成友]
        ///Create Date： [2010-5-21]
        ///</summary>
        public string CallBackGroupNo(string vUserNo)
        {
            try
            {
                vUserinfo = Nova_DAL_Users.CallBackGroupNo(vUserNo);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return vUserinfo;
        }

        /// <summary>
        /// 根据id查询用户表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回DataSet</returns>
        /// writer:[Kitty]
        /// Create Date:[2010-5-28]
        public DataSet GetUserDataSet(string id)
        {
            try
            {
                ds = Nova_DAL_Users.QueryUserDataSet(id);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 根据id查询用户表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回DataSet</returns>
        /// writer:[Kitty]
        /// Create Date:[2010-5-28]
        public DataSet GetUserDataSetByNo(string UserNo)
        {
            try
            {
                ds = Nova_DAL_Users.QueryUserDataSetByNo(UserNo);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }
        /// <summary>
        /// 查询所有的用户资料
        /// </summary>
        /// <returns>返回DataSet</returns>
        /// writer:[Kitty]
        /// Create Date:[2010-5-28]
        public DataSet GetAllUserDataSet()
        {
            try
            {
                ds = Nova_DAL_Users.QueryAllUserDataSet();
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 根据条件查询用户资料
        /// </summary>
        /// <param name="vUserNo"></param>
        /// <param name="vUserName"></param>
        /// <param name="strDept"></param>
        /// <returns>返回DataSet</returns>
        /// writer:[Kitty]
        /// Create Date:[2010-5-28]
        public DataSet GetUserDataSet(string vUserNo, string vUserName, string strDept)
        {
            try
            {
                ds = Nova_DAL_Users.QueryUserDataSet(vUserNo, vUserName, strDept);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;

        }

        ///<summary>
        ///增加用户表
        ///</summary>
        ///<param name="sQL"></param>
        ///<returns>返回int,0失败,1成功,2已经存在用户编号</returnss>
        /// writer:[Kitty]
        /// Create Date:[2010-5-28]
        ///</summary>
        public int AddUser(Nova_MOD.Users Users)
        {
            if (Nova_DAL_Users.CheckUserNoExists(Users.vUserNo) > 0)
            {
                i = 2;
            }
            else
            {
                try
                {

                    i = Nova_DAL_Users.AddUser(Users);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                }
            }
            return i;
        }

        public bool UpdateUserMaterialGroupRelation(string userNo,string newUserNo, int materialGroupType)
        {
            TUserMaterialGroupRelationBll bll = new TUserMaterialGroupRelationBll();
            var model = bll.GetByUserNo(userNo);
            if(model.IsEmpty())
            {
                TUserMaterialGroupRelation entity = new TUserMaterialGroupRelation();
                entity.UserNo = newUserNo;
                entity.MaterialGroupType = materialGroupType;
                entity.CreateTime = DateTime.Now;
                entity.UpdateTime = DateTime.Now;
                return bll.InsertEntity(entity);
            }
            else
            {
                model.UserNo = newUserNo;
                model.MaterialGroupType = materialGroupType;
                model.UpdateTime = DateTime.Now;
                return bll.UpdateEntity(model);
            }
        }

        ///<summary>
        ///修改用户表
        ///</summary>
        ///<param name="sQL"></param>
        ///<returns>返回int,0失败,1成功,2已经存在该用户编号</returnss>
        /// writer:[Kitty]
        /// Create Date:[2010-5-28]
        ///</summary>
        public int UpdateUser(Nova_MOD.Users Users, string oldUserNo)
        {
            if (Nova_DAL_Users.CheckUserNoExists(Users.vUserNo, oldUserNo) > 0)
            {
                i = 2;
            }
            else
            {
                try
                {
                    i = Nova_DAL_Users.UpdateUser(Users, oldUserNo);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                }
            }
            return i;
        }

        ///<summary>
        ///删除多个用户
        ///</summary>
        ///<param name="sQL">id</param>
        ///<returns>返回int,0失败,1成功</returnss>
        /// writer:[Kitty]
        /// Create Date:[2010-5-28]
        ///</summary>
        public int DeleteUsers(string listId)
        {
            try
            {
                i = Nova_DAL_Users.DeleteUsers(listId);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return i;
        }

        ///<summary>
        ///注销多个用户
        ///</summary>
        ///<param name="sQL">id</param>
        ///<returns>返回int,0失败,大于0成功</returnss>
        /// writer:[Kitty]
        /// Create Date:[2010-5-29]
        ///</summary>
        public int LogoutUsers(string listId)
        {
            try
            {
                i = Nova_DAL_Users.LogoutUsers(listId);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return i;
        }

        ///<summary>
        ///修改密码
        ///</summary>
        ///<param name="sQL"></param>
        ///<returns>返回int,0失败,1成功</returnss>
        /// writer:[夏鑫]
        /// Create Date:[2010-5-28]
        ///</summary>
        public int UpdateUserPwd(Nova_MOD.Users Users)
        {
            try
            {
                i = Nova_DAL_Users.UpdateUserPwd(Users);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return i;
        }

        /// <summary>
        /// 根据用户名和密码获取用户信息
        /// </summary>
        /// <param name="userNo"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public DataTable GetByUserNoPassword(string userNo, string password)
        {
            return Nova_DAL_Users.GetByUserNoPassword(userNo, password);
        }

    }
}
